﻿#region MIT Licence
/*
 * Copyright (C) 2014 PdfCreative
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom 
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using PaintDotNet;
using PaintDotNet.Effects;

namespace PdfCreative.Filters.ConvertToTransparent
{
    /// <summary>
    /// The ConvertToTransparentPlugin class is a Paint .NET effect plug-in. It inherits
    /// from the Effect class.
    /// The ConvertToTransparentPlugin class alters an image by turning each pixel into
    /// a transparent pixel, using the pixel's lightness (the Value component of its HSV
    /// representation) as key to the transparency.
    /// </summary>
    public class ConvertToTransparentPlugin : PaintDotNet.Effects.Effect
    {
        // Public properties
        // =================
        #region Static effect properties
        /// <summary>
        /// The name of the plug-in.
        /// </summary>
        public static string StaticName
        {
            get
            {
                return "Convert to transparent";
            }
        }

        /// <summary>
        /// The plug-in's icon.
        /// </summary>
        public static Bitmap StaticImage
        {
            get
            {
                return Properties.Resources.Icon;
            }
        }

        /// <summary>
        /// The submenu to display the plug-in under.
        /// </summary>
        public static string StaticSubMenuName
        {
            get
            {
                return null;
            }
        }
        #endregion


        // Base class overrides
        // ====================
        #region Effect overrides
        /// <summary>
        /// Renders the effect. This method turns every pixel of
        /// every Rectangle of Interest into a transparent pixel.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <param name="dstArgs">The destination arguments.</param>
        /// <param name="srcArgs">The source arguments.</param>
        /// <param name="rois">The rectangles of interests.</param>
        /// <param name="startIndex">The index in the array of rectangles at which to start.</param>
        /// <param name="length">The number of rectangles in the array to go through.</param>
        public override void Render(
            EffectConfigToken parameters, 
            RenderArgs dstArgs, 
            RenderArgs srcArgs, 
            Rectangle[] rois, 
            int startIndex, 
            int length)
        {
            // We can use a single instance of the filter for all
            // rectangles.
            var filter = new Filter();

            // Go through each rectangle in the array (constrained by
            // the 'startIndex' and 'length' arguments) and execute the
            // filter against the rectangle.
            for (int i = startIndex; i < startIndex + length; ++i)
            {
                Rectangle rect = rois[i];

                filter.Execute(srcArgs.Surface, dstArgs.Surface, rect);
            }
        }
        #endregion



        // Public initialiser
        // ==================
        #region Public initialiser
        /// <summary>
        /// Initialises a new ConvertToTransparentPlugin instance.
        /// </summary>
        public ConvertToTransparentPlugin()
            : base(ConvertToTransparentPlugin.StaticName,
                ConvertToTransparentPlugin.StaticImage,
                ConvertToTransparentPlugin.StaticSubMenuName,
                EffectFlags.None)
        {
        }
        #endregion

    } // ConvertToTransparentPlugin class
}
